//
//  ViewController.m
//  UI20_网络数据请求作业
//
//  Created by dllo on 16/4/6.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "ViewController.h"
#import "FDMTableViewCell.h"
#import "dataModel.h"
#import "NetWorkHandle.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, NetWorkHandleDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *modelArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[FDMTableViewCell class] forCellReuseIdentifier:@"FDMTableViewCell"];
    [self dataHandle];
}

- (void)dataHandle {
    NSString *url = @"http://mobile.ximalaya.com/m/explore_album_list?category_name=all&condition=hot&device=android&page=1&per_page=20&status=0&tag_name=%E5%8D%81%E4%BA%8C%E6%98%9F%E5%BA%A7";
    
//    NSURLSession *session = [NSURLSession sharedSession];
//    
//    NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"%@", result);
//        self.modelArr = [NSMutableArray array];
//        for (NSDictionary *dic in result[@"newsList"]) {
//            dataModel *model = [[dataModel alloc] init];
//            [model setValuesForKeysWithDictionary:dic];
//            [self.modelArr addObject:model];
//        }
//
//        [self.tableView reloadData];
//    }];
//    [task resume];
    
    [NetWorkHandle netWorkHandleWithUrlString:url netWorkDelegate:self];
}

- (void)finishNetWorkHandleWithData:(id)result {
    self.modelArr = [NSMutableArray array];
    for (NSDictionary *dic in result[@"list"]) {
        dataModel *model = [[dataModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.modelArr addObject:model];
    }
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FDMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FDMTableViewCell"];
    dataModel *model = self.modelArr[indexPath.row];
    
    [cell setDataModel:model];
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
